﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using System;
using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class ManipulateComments
	{
		private readonly Random _random = new Random();
		private readonly string[] _strings = new[] { "one", "two", "three", "four", "five" };
		private readonly Uri[] _uris = new[]
		{
			new Uri("http://www.microsoft.com/"),
			new Uri("http://www.google.com/"),
			new Uri("http://www.yahoo.com"),
			new Uri("http://www.youtube.com"),
			new Uri("http://www.bbc.co.uk/")
		};

		[Test]
		public void SetAndReadCommentText()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						var comment = _strings[_random.Next(5)];
						part.Worksheet.SetCommentText(address, comment);
						Assert.AreEqual(comment, part.Worksheet.GetCommentText(address), "Retrieved comment text");
					}
				}

				Assert.AreEqual(100, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
				Assert.AreEqual(1, part.VmlDrawingParts.Count, "Vml drawing count after");
				Assert.NotNull(part.Worksheet.LegacyDrawing.Id, "Legacy drawing id after");
			}
		}

		[Test]
		public void SetAndClearCommentText()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentText(address, _strings[_random.Next(5)]);
					}
				}

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentText(address, null);
						Assert.IsNull(part.Worksheet.GetCommentText(address), "Retrieved comment text");
					}
				}

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
				Assert.False(part.VmlDrawingParts[0].Drawing.IsPopulated(), "Vml drawing populated after");
			}
		}

		[Test]
		public void SetAndReadCommentRichText()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						var comment = _strings[_random.Next(5)];
						part.Worksheet.SetCommentRichText(address, comment);
						Assert.AreEqual(comment, part.Worksheet.GetCommentRichText(address), "Retrieved comment text");
					}
				}

				Assert.AreEqual(100, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
				Assert.AreEqual(1, part.VmlDrawingParts.Count, "Vml drawing count after");
				Assert.NotNull(part.Worksheet.LegacyDrawing.Id, "Legacy drawing id after");
			}
		}

		[Test]
		public void SetAndClearCommentRichText()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentRichText(address, _strings[_random.Next(5)]);
					}
				}

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentText(address, null);
						Assert.IsNull(part.Worksheet.GetCommentRichText(address), "Retrieved comment text");
					}
				}

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
				Assert.False(part.VmlDrawingParts[0].Drawing.IsPopulated(), "Vml drawing populated after");
			}
		}

		[Test]
		public void DeleteRowsContainingComments()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentText(address, _strings[_random.Next(5)]);
					}
				}

				Assert.AreEqual(100, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				part.Worksheet.DeleteRows(0, 10);

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
			}
		}

		[Test]
		public void DeleteColumnsContainingComments()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var row = 0; row < 10; row++)
				{
					for (var col = 0; col < 10; col++)
					{
						var address = new GridReference(row, col);
						part.Worksheet.SetCommentText(address, _strings[_random.Next(5)]);
					}
				}

				Assert.AreEqual(100, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				part.Worksheet.DeleteColumns(0, 10);

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count after");
			}
		}

		[Test]
		public void DeleteColumnsLeftOfComments()
		{
			var path = Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource);

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var row = 0; row < 10; row++)
				{
					var address = new GridReference(row, 5);
					part.Worksheet.SetCommentText(address, _strings[_random.Next(5)]);
				}

				Assert.AreEqual(10, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				part.Worksheet.DeleteColumns(2, 2);
			}

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(10, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var row = 0; row < 10; row++)
				{
					var address = new GridReference(row, 3);
					Assert.NotNull(part.Worksheet.GetCommentText(address), "Retrieved comment text");
				}
			}
		}

		[Test]
		public void DeleteRowsAboveComments()
		{
			var path = Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource);

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(0, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var col = 0; col < 10; col++)
				{
					var address = new GridReference(5, col);
					part.Worksheet.SetCommentText(address, _strings[_random.Next(5)]);
				}

				Assert.AreEqual(10, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count before");

				part.Worksheet.DeleteRows(2, 2);
			}

			using (var package = SpreadsheetDocumentWrapper.Open(path))
			{
				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.AreEqual(10, part.WorksheetCommentsPart.Comments.CommentList.Comments.Count, "Comment count initial");

				for (var col = 0; col < 10; col++)
				{
					var address = new GridReference(3, col);
					Assert.NotNull(part.Worksheet.GetCommentText(address), "Retrieved comment text");
				}
			}
		}
	}
}
